/*
 * test_levent_read.cpp
 *
 *  Created on: Apr 2, 2014
 *      Author: killerg
 */

#include <assert.h>
#include <stdio.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>

static void read_cb(int fd, short events, void *ctx) {
	if (events & EV_READ) {
		evbuffer* buffer = evbuffer_new();
		evbuffer_read(buffer, fd, -1);
		evbuffer_write_atmost(buffer, 1, -1);
		evbuffer_free(buffer);
	} else if (events & EV_TIMEOUT) {
		printf("timeout\n");
	}
}

bool test_levent_read() {

	event_base* base = event_base_new();
	assert(base);

	evutil_make_socket_nonblocking(0);
	evutil_make_socket_nonblocking(1);
	event* evi = event_new(base, 0, EV_READ|EV_PERSIST, read_cb, NULL);
	timeval tv = {1, 0};
	event_add(evi, &tv);
	event_base_loop(base, 0);
	event_free(evi);

	event_base_free(base);

	return true;
}
